package pers.cz.tools;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: PostGirl-panent
 * @description: 字符串工具类
 * @author: Cheng Zhi
 * @create: 2022-06-28 09:35
 **/
public class StringUtils {

    /**
     * 该方法可以自动去除分割结果中元素的前后空格
     * @param str
     * @param symbol
     * @return
     */
    public static String[] split(String str, String symbol) {

        String[] results = str.split(symbol);
        String[] newResult = new String[results.length];
        for (int i=0; i< results.length; i++) {
            newResult[i] = results[i].trim();
        }
        results = null;

        return newResult;
    }
    /**
     * 将String转换为Boolean,如果转换不成功，则返回默认类型
     * @param s
     * @param defaultValue
     * @return
     */
    public static Boolean toBoolean(String s, Boolean defaultValue) {

        if ("TRUE".equalsIgnoreCase(s) || "1".equals(s) || "Y".equalsIgnoreCase(s) || "ON".equalsIgnoreCase(s)) {
            return true;
        }

        if ("FALSE".equalsIgnoreCase(s) || "0".equalsIgnoreCase(s) || "N".equalsIgnoreCase(s) || "OFF".equalsIgnoreCase(s)) {
            return false;
        }

        if (defaultValue == null) {
            // 如果不指定默认值，且没有符合上述判断条件的，那么认为这个s不适合转换为Boolean,因此要抛出异常。
            throw new IllegalArgumentException(s + " can not be case to boolean.");
        }

        return defaultValue;
    }

    /**
     * 字符替换
     * @param str
     * @param searchChars
     * @param replaceChars
     * @return
     */
    public static String replaceChars(String str, String searchChars, String replaceChars) {
        if (!isEmpty(str) && !isEmpty(searchChars)) {
            if (replaceChars == null) {
                replaceChars = "";
            }

            boolean modified = false;
            int replaceCharsLength = replaceChars.length();
            int strLength = str.length();
            StringBuilder buf = new StringBuilder(strLength);

            for(int i = 0; i < strLength; ++i) {
                char ch = str.charAt(i);
                int index = searchChars.indexOf(ch);
                if (index >= 0) {
                    modified = true;
                    if (index < replaceCharsLength) {
                        buf.append(replaceChars.charAt(index));
                    }
                } else {
                    buf.append(ch);
                }
            }

            if (modified) {
                return buf.toString();
            } else {
                return str;
            }
        } else {
            return str;
        }
    }

    public static boolean isEmpty(CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

    /**
     * 将字符串按照指定的字符分割为集合
     * @param strs
     * @param symbol
     * @return
     */
    public static List<String> stringToList(String strs, String symbol) {

        String[] split = split(strs,symbol);
        List<String> strList = new ArrayList<>();
        for (String str : split) {
            strList.add(str);
        }

        return strList;
    }

    /**
     * 获取指定字符分割后的去除最后一个分割符之后的内容
     * @param strs
     * @param symbol
     * @return
     */
    public static String getNoLast(String strs, String symbol) {

        String[] splits = split(strs, symbol);

        return null;
    }
    public static void main(String[] args) {
        String a = "  a , b,  c  , d   , e";
        String[] split = split(a, ",");
        for (String str : split) {
            System.out.println(str);
        }
    }
}
